﻿using System.Collections.Generic;
using System.IO;

namespace DGMResourcesTool.Logic.FileUtil
{
    public class FileUtility
    {
        /// <summary>
        /// 根据名称获取配置文件的目录.
        /// </summary>
        /// <returns></returns>
        public static string GetConfigFilePath(string configFileName, bool willCreate = false)
        {
            string filePath = Path.GetFullPath(@"..\..\..\config\") + configFileName;
            if (!System.IO.File.Exists(filePath))
            {
                // 如果不存在则创建.
                if (willCreate)
                {
                    FileStream fileStream = System.IO.File.Create(filePath);
                    fileStream.Close();
                    return filePath;
                }

                return string.Empty;
            }

            return filePath;
        }

        #region 拼接文件扩展名

        /// <summary>
        /// 根据资源类型获取包含扩展名的文件名称或路径.
        /// </summary>
        public static bool GetFilePathWithExtension(EnumResType resType, ref string filePath)
        {
            switch (resType)
            {
                case EnumResType.Invalid:
                    return false;
                case EnumResType.All:
                    if (GetFilePathWithTextureExtension(ref filePath))
                    {
                        return true;
                    }
                    if (GetFilePathWithPrefabExtension(ref filePath))
                    {
                        return true;
                    }
                    return false;
                case EnumResType.Texture:
                    return GetFilePathWithTextureExtension(ref filePath);
                case EnumResType.Effect:
                    return GetFilePathWithPrefabExtension(ref filePath);
            }

            return false;
        }

        /// <summary>
        /// 图片类型扩展名拼接.
        /// </summary>
        private static bool GetFilePathWithTextureExtension(ref string filePath)
        {
            if (System.IO.File.Exists(filePath + ".png"))
            {
                filePath += ".png";
                return true;
            }

            if (System.IO.File.Exists(filePath + ".jpg"))
            {
                filePath += ".jpg";
                return true;
            }

            return false;
        }

        /// <summary>
        /// 预设类型扩展名拼接.
        /// </summary>
        private static bool GetFilePathWithPrefabExtension(ref string filePath)
        {
            if (System.IO.File.Exists(filePath + ".prefab"))
            {
                filePath += ".prefab";
                return true;
            }

            return false;
        }

        #endregion

        #region 获得特殊类型文件

        /// <summary>
        /// 获得文件对应的.h3dmanifest文件.
        /// </summary>
        public static string GetH3DManifestFilePath(string filePath)
        {
            string manifestFilePath = filePath + ".h3dmanifest";
            if (File.Exists(manifestFilePath))
            {
                return manifestFilePath;
            }
            return string.Empty;
        }

        /// <summary>
        /// 获得文件对应的.meta文件.
        /// </summary>
        public static string GetMetaFilePath(string filePath)
        {
            string metaFilePath = filePath + ".meta";
            if (File.Exists(metaFilePath))
            {
                return metaFilePath;
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取动作文件对应的表情文件.
        /// </summary>
        public static string GetExpressionFromActionFilePath(string filePath)
        {
            string expressionFilePath = string.Empty;
            if (filePath.EndsWith("m"))
            {
                string fileName = Path.GetFileNameWithoutExtension(filePath);
                expressionFilePath = filePath.Replace(fileName, "0000000101_" + fileName);
            }

            if (filePath.EndsWith("f"))
            {
                string fileName = Path.GetFileNameWithoutExtension(filePath);
                expressionFilePath = filePath.Replace(fileName, "1000000101_" + fileName);
            }

            if (!string.IsNullOrEmpty(expressionFilePath))
            {
                string[] pathStrings = expressionFilePath.Split('\\');
                string actionDirectoryStr = pathStrings[pathStrings.Length - 2];
                expressionFilePath = expressionFilePath.Replace(actionDirectoryStr, actionDirectoryStr + "_expression");
            }

            if (File.Exists(expressionFilePath))
            {
                return expressionFilePath;
            }
            return string.Empty;
        }

        #endregion
    }
}